package com.celesea.auth.dept.v1.controller;

import com.celesea.auth.dept.service.DeptService;
import com.celesea.auth.dept.v1.controller.request.PostRequest;
import com.celesea.auth.dept.v1.controller.request.PutRequest;
import com.celesea.auth.dept.v1.controller.resource.DeptResource;
import com.celesea.auth.dept.v1.controller.resource.DeptTreeResource;
import com.celesea.auth.dept.vo.DeptTreeVo;
import com.celesea.auth.dept.vo.DeptVo;
import com.celesea.auth.role.entity.RoleCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-10-28 21:17
 */
@RestController
@RequestMapping("v1/depts")
@Api(tags = "部门接口")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    @ApiOperation(value = "部门树")
    public List<DeptTreeResource> index() {
        List<DeptTreeVo> lists=deptService.selectDepts();
        List<DeptTreeResource> results=
                lists.stream().map(item->new DeptTreeResource(item))
                        .collect(Collectors.toList());
        return results;
    }

    @PostMapping
    @ApiOperation(value = "保存部门")
    @PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
    public DeptResource post(@RequestBody @Valid PostRequest request) {
        DeptVo dept = deptService.insert(new DeptVo(request));
        return new DeptResource(dept);
    }

    @PutMapping
    @ApiOperation(value = "更新部门")
    @PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
    public DeptResource put(@RequestBody @Valid PutRequest request) {
        DeptVo dept = deptService.update(new DeptVo(request));
        return new DeptResource(dept);
    }

    @GetMapping(value = "/{parentId}/child")
    @ApiOperation(value = "根据parentId构建部门")
    @PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
    public DeptResource child(@PathVariable String parentId) {
        DeptVo dept = deptService.buildById(parentId);
        return new DeptResource(dept);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询部门")
    @PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
    public DeptResource get(@PathVariable String id) {
        DeptVo dept = deptService.selectById(id);
        return new DeptResource(dept);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据id删除部门")
    @PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
    public void delete(@PathVariable String id) {
        deptService.doDelete(id);
    }

    @PutMapping(value = "/{id}/fixed")
    @ApiOperation(value = "根据id修复归属公司信息，修复对象为子部门和员工")
    @PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
    public void fixed(@PathVariable String id) {
        deptService.updateFixCompany(id);
     }

}
